<?php
class articleModel extends RPHP_Model
{

    //消息列表
    public function post_news($page = 1, $pagesize = 10)
    {
        $where = " 1=1 ";

        $sql = "SELECT COUNT(`article_id`) AS `cnt` FROM {$this->_prefix['primary']}article WHERE " . $where;

        $cnt = $this->db->getone($sql);
        if ($cnt > 0) {

            $start = ($page - 1) * $pagesize;
            //默认排序
            $orderby = "`add_time` DESC,`article_id` DESC";

            $sql = "SELECT * FROM {$this->_prefix['primary']}article WHERE " . $where . " ORDER BY " . $orderby . " LIMIT " . $start . "," . $pagesize;

            $rs = $this->sql($sql);

            foreach ($rs as $k => $value) {
                //时间
                $rs[$k]['add_time'] = date('H:i', $value['add_time']);
                //标题
                $rs[$k]['keywords'] = strip_tags(htmlspecialchars_decode(trim($value['keywords'])));
                //内容
                $rs[$k]['content'] = strip_tags(htmlspecialchars_decode(trim($value['content'])));

                if ($value['pic_docids'] > 0) {
                    $pic_docids           = $value['pic_docids'];
                    $img                  = $this->getDocs($pic_docids);
                    $rs[$k]['pic_docids'] = $img[$pic_docids];
                } else {
                    $rs[$k]['pic_docids'] = "/style/images/home/tuijian1.png";
                }

                $sql                 = "SELECT `name` FROM {$this->_prefix['primary']}article_category WHERE `caid`=" . $value['caid'];
                $rs[$k]['classname'] = $this->db->getone($sql);

            }

        }
        return array(
            'page'    => $page,
            'maxpage' => ceil($cnt / $pagesize),
            'count'   => $cnt,
            'data'    => $rs,
        );

    }

    //获取新闻详细
    public function getShow($id)
    {
        if ($id) {
            $sql              = "SELECT * FROM {$this->_prefix['primary']}article WHERE article_id=" . $id;
            $rs               = $this->db->getrow($sql);
            $img              = $this->getDocs($rs['pic_docids']);
            $rs['pic_docids'] = $img[$rs['pic_docids']];
            //内容
            $rs['content'] = htmlspecialchars_decode(trim($rs['content']));
        }
        return array(
            'data' => $rs,
        );
    }

    //get document
    public function getDocs($docid)
    {
        if (is_array($docid)) {
            $where = " WHERE `docid` IN (" . implode(",", array_unique($docid)) . ")";
        } else {
            $where = " WHERE `docid`=" . intval($docid);
        }

        $sql = "SELECT `docid`,`filename`,`create_time` FROM {$this->_prefix['primary']}documents " . $where;
        $rs  = $this->sql($sql);
        if ($rs) {
            foreach ($rs as $r) {
                $doc[$r['docid']] = $this->c['doc_host'] . date("Ymd", $r['create_time']) . "/" . date("H", $r['create_time']) . "/" . date("i", $r['create_time']) . "/" . $r['filename'];

            }
            return $doc;
        } else {
            return false;
        }

    }
}
